package com.dongdongshop.front.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.front.pojo.TbSeckillGoods;
import com.dongdongshop.front.pojo.TbSeckillOrder;
import com.dongdongshop.front.pojo.TbUser;
import com.dongdongshop.front.service.SeckillGoodsService;
import com.dongdongshop_util.Result;
import com.dongdongshop_util.ResultEnum;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("seckillGoods")
public class SeckillGoodsController {

    private final Logger logger = LoggerFactory.getLogger(SeckillGoodsController.class);

    @Reference(check = false,timeout = 500000)
    private SeckillGoodsService seckillGoodsService;
    @Autowired
    private RedisTemplate redisTemplate;

    //跳转到秒杀页
    @RequestMapping("toSeckillIndex")
    public String toSeckillIndex(Model model){
        List<TbSeckillGoods> seckillGoodsList = seckillGoodsService.getSeckillGoods();
        model.addAttribute("seckillGoodsList",seckillGoodsList);
        return "admin/seckill-index";
    }
    //根据商品id查询跳转商品详情页
    @RequestMapping("getSeckillById/{id}")
    public String getSeckillById(@PathVariable("id") Long id,Model model){
        TbSeckillGoods seckillGoods = seckillGoodsService.getSeckillById(id);
        if (seckillGoods == null){
            return "admin/seckillover";
        }
        model.addAttribute("seckillGoods",seckillGoods);
        return "admin/seckill-item";
    }

    //秒杀立即抢购
    @ResponseBody
    @RequestMapping("submitorder")
    public Result submitorder(Long seckillId){
        //判断登录用户是否登录
       TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
       if (user == null){
           return Result.response(ResultEnum.UNAUTH);
       }
       //读取用户id
        String username = user.getUsername();
       try {
           seckillGoodsService.submitorder(seckillId,username);
           return Result.success(ResultEnum.SUCCESS,username);
       }catch (Exception e){
           logger.error(String.valueOf(ResultEnum.FAIT),e);
           return  Result.response(ResultEnum.FAIT);
       }
    }

    @ResponseBody
    @RequestMapping("findOrder")
    public Result findOrder(Long seckillId,String username){

        try {
            TbSeckillOrder seckillOrder = seckillGoodsService.findOrder(seckillId,username);
            if (seckillOrder != null){
                return Result.success(ResultEnum.SUCCESS,"ok");
            }
            return Result.success(ResultEnum.SUCCESS,"正在生成订单");
        }catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return  Result.response(ResultEnum.FAIT);
        }

    }
}
